package com.example.slidecaptcha2;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 *
 * @author yyf900524
 * @date 2019/12/17
 */
@Component
public class RedisUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);

    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;


    /**
     * 设置String类型的value和过期时间
     *
     * @param key
     * @param value
     * @param timeout
     * @throws Exception
     */
    public void setString(String key, String value, Integer timeout) {
        redisCacheTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }


    /**
     * 获取String类型的value
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return (String) redisCacheTemplate.opsForValue().get(key);
    }


    /**
     * 设置Object类型的value和过期时间
     *
     * @param key
     * @param value
     * @param seconds
     * @throws Exception
     */
    public void setObject(String key, Serializable value, Integer seconds) {
        redisCacheTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    }

    public void setObject(String key, Serializable value) {
        redisCacheTemplate.opsForValue().set(key, value);
    }


    /**
     * 获取Object类型的value
     *
     * @param key
     * @return
     */
    public Serializable getObject(String key) {
        return redisCacheTemplate.opsForValue().get(key);
    }


    /**
     * 删除key
     *
     * @param key
     * @throws Exception
     */
    public void delKey(String key) {
        redisCacheTemplate.delete(key);
    }

    /**
     * 删除key
     *
     * @param keys
     * @throws Exception
     */
    public void delKeys(Set<String> keys) {
        redisCacheTemplate.delete(keys);
    }


    /**
     * 更新过期时间
     *
     * @param key
     * @param seconds
     * @throws Exception
     */
    public void updateTime(String key, Integer seconds) {
        redisCacheTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }


    /**
     * redis分布式锁 - 加锁
     *
     * @param key
     * @param value
     * @return
     * @throws Exception
     */
    public boolean lock(String key, String value) {
        if (redisCacheTemplate.opsForValue().setIfAbsent(key, value)) {
            return true;
        }
        String currentValue = getString(key);
        if (StringUtils.isNotBlank(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()) {
            String oldValue = (String) redisCacheTemplate.opsForValue().getAndSet(key, value);
            if (StringUtils.isNotBlank(oldValue) && oldValue.equals(currentValue)) {
                return true;
            }
        }
        return false;
    }


    /**
     * redis分布式锁 - 解锁
     *
     * @param key
     * @param value
     * @throws Exception
     */
    public void unlock(String key, String value) {
        if (value.equals(getString(key))) {
//            redisCacheTemplate.opsForValue().getOperations().delete(key);
            redisCacheTemplate.delete(key);
        }
    }
}
